<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理-添加</title>
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
</head>

<body>
<div class="x-body">
    <form id="formAdd" class="layui-form">//avatar
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>用户账号</label>
            <div class="layui-input-inline">
                <input type="text" id="account" name="account" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><span class="x-red">*</span>必填 可用于登录</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>用户姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><span class="x-red">*</span>必填 可用于登录</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>登录密码</label>
            <div class="layui-input-inline">
                <input type="password" id="password" name="password" required lay-verify="pass" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><span class="x-red">*</span>必填 6到16个字符</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>确认密码</label>
            <div class="layui-input-inline">
                <input type="password" id="rePassword" required lay-verify="pass" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><span class="x-red">*</span>必填 6到16个字符</div>
        </div>
        <div class="layui-form-item openness">
            <label class="layui-form-label"><span class="x-red">*</span>用户性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="1" title="男性" lay-skin="primary" checked />
                <input type="radio" name="sex" value="2" title="女性" lay-skin="primary" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>邮箱</label>
            <div class="layui-input-inline">
                <input type="text" id="email" name="email" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"><span class="x-red">*</span>必填 可用于登录</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>生日</label>
            <div class="layui-input-inline">
                <input type="text" id="date" name="date" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>内网IP</label>
            <div class="layui-input-inline">
                <input type="text" id="i_p" name="i_p" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>兴趣爱好</label>
            <div class="layui-input-inline">
                <input type="text" id="version" name="version" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>用户手机</label>
            <div class="layui-input-inline">
                <input type="text" id="phone" name="phone" required lay-verify="required" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">可用于登录</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" for="code"><span class="x-red">*</span>验证码：</label>
            <div >
                <input type="text" id="code"  style="width:20%" required lay-verify="required" class="layui-input layui-inline">
                <input class="layui-btn layui-btn-warm  layui-btn-sm layui-inline get-code" style="width:20%" type="button" id="J_getCode" value="获取验证码"/>
                <button class="layui-btn layui-btn-sm layui-btn-disabled layui-inline reset-code"  id="J_resetCode" style="display:none; width:20%"><span id="J_second">60</span>秒后重发</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <input class="layui-btn" type="button" id="add"  value="增加" />
        </div>
    </form>
</div>
</body>
</html>
<script src="${ctxPath}/static/manage/layui/layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/manage/js/x-layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/manage/js/jquery.js"></script>
<script src="${ctxPath}/static/manage/js/util.js"></script>

<script type="text/javascript">
    layui.use(['form','layer','laydate'], function(){
        var $ = layui.$, form = layui.form,laydate=layui.laydate;
        laydate.render({
            elem: '#date'
        });
        var code="0_0";
        /*获取验证码*/
        var isPhone = 1;
        $('#J_getCode').on('click', function() {
            checkPhone(); //验证手机号码
            if (isPhone){
                resetCode(); //倒计时
                SelCode();//验证码
                alert("已发送，1分钟内有效，注意查收");
            } else {
                $('#phone').focus();
            }
        });

        //发送验证码（调用接口）
        function SelCode() {
            $.ajax({
                url:'/sms',
                type:'get',
                data:{
                    phone:$('#phone').val()
                },
                success:function(result){
                    if(result != "0"){
                    }
                    code=result;
                },
            });
        }

        //邮箱通知
        function SendEmail(email) {
            $.ajax({
                url:'/SendEmail',
                type:'get',
                data:{
                    email:email
                },
                success:function(result){
                }
            });
        }
        //验证手机号码
        function checkPhone(){
            var phone = $('#phone').val();
            var pattern = /^1[0-9]{10}$/;
            isPhone = 1;
            if(phone == '') {
                alert('请输入手机号码');
                isPhone = 0;
                return;
            }
            if(!pattern.test(phone)){
                alert('请输入正确的手机号码');
                isPhone = 0;
                return;
            }
        }
        //倒计时
        function resetCode(){
            $('#J_getCode').hide();
            $('#J_second').html('59');
            $('#J_resetCode').show();
            var second = 59;
            var timer = null;
            timer = setInterval(function(){
                second -= 1;
                if(second >0){
                    $('#J_second').html(second);
                }else{
                    clearInterval(timer);
                    $('#J_getCode').show();
                    $('#J_resetCode').hide();
                }
            },1000);
        }

        // 信息提交
        $("#add").on('click', function() {
            var phone = $("#phone").val();
            var email = $("#email").val();
            var password = $("#password").val();
            var rePassword = $("#rePassword").val();
            // 校验俩次密码是否一致，校验手机号是否合法
            if(password != rePassword){
                alert("俩次密码不一致，请重新输入");
                return false;
            }
            if(!checkMobile(phone)){
                alert("手机号码不合法");
                return false;
            }
            if(email.length>30){
                alert("邮箱地址长度不能超过30个字符");
                return false;
            }
            if(!checkEmail(email)){
                alert("邮件地址不合法");
                return false;
            }
            var signCode=$("#code").val();
                if(code==signCode){
                    //emailVerification(email);
                    //验证邮箱真实性
                    $.ajax({
                        url:'/emailVerification',
                        type:'get',
                        data:{
                            email:email
                        },
                        success:function(result){
                            if(result=="200"){
                                SendEmail(email)
                                $.ajax({
                                    url:"/signUserAdd",
                                    type:'post',
                                    data:$("#formAdd").not("#code").serialize(),
                                    success:function(result){
                                        alert(result);
                                        if(result=="处理成功"){
                                            $.ajax({
                                                url:'/smsAdvice',
                                                type:'get',
                                                data:{
                                                    phone:$('#phone').val()
                                                },
                                                success:function(result){
                                                    if(result != "0"){
                                                        alert("短信接口异常，请联系管理员，错误码："+result)
                                                        window.parent.location.reload();
                                                    }else {
                                                        window.parent.location.reload();
                                                    }
                                                },
                                            });
                                        }
                                    },
                                    error:function(result){
                                        alert("失败？请及时告知开发人员"+result);
                                        window.parent.location.reload();
                                    }
                                });
                            }else if(result=="203"){
                                alert("您的邮箱"+email+"并不存在，请填写真实的邮箱")
                            }else{
                            }
                        }
                    });
                }else{
                    alert("验证码错误")
                }
        });
    });
</script>

